package com.sonicboom.nurun.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.representation.Form;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.apache.log4j.Logger;

public class DDMSClient {

    private static final Logger logger = Logger.getLogger(DDMSClient.class);

    public DDMSClient() {

    }

    public DDMSResult login(String userName, String password) {
        logger.info(String.format("user name : [%s]; password:[%s]", userName, password));
        Form form = new Form();
        form.add("name", userName);
        form.add("passwd", password);
        ClientResponse response = Client.create().resource("http://www.dangdaimingshi.com/ajax/201408ugc/login").post(ClientResponse.class, form);

        String result = response.getEntity(String.class);

        logger.error("Response is:" + result);
        if (response.getStatus() != 200) {

            return new DDMSResult("400", "网络异常，请稍后重试");
        } else {
            JSONObject json = (JSONObject) JSONSerializer.toJSON(result);
            String status = json.getString("status");
            switch (status) {
                case "1":
                    return new DDMSResult("200", "登录成功");
                case "030":
                    return new DDMSResult("030", "用户名不能为空");
                case "031":
                    return new DDMSResult("031", "密码不能为空");
                case "032":
                    return new DDMSResult("032", "用户名或密码错误");
                case "033":
                    return new DDMSResult("033", "邮箱不能为空");
                case "034":
                    return new DDMSResult("034", "密码不匹配");
                case "035":
                    return new DDMSResult("035", "用户名已经存在");
                case "036":
                    return new DDMSResult("036", "邮箱已经被使用");
                case "999":
                    return new DDMSResult("999", "未知错误");
                default:
                    return new DDMSResult("999", "未知错误");
            }
        }


    }

    public DDMSResult register(String userName, String password, String email) {
        logger.info(String.format("user name : [%s]; password:[%s]", userName, password));
        Form form = new Form();
        form.add("name", userName);
        form.add("email", email);
        form.add("passwd", password);
        form.add("repasswd", password);

        ClientResponse response = Client.create().resource("http://www.dangdaimingshi.com/ajax/201408ugc/register").post(ClientResponse.class, form);

        String result = response.getEntity(String.class);

        logger.error("Response is:" + result);
        if (response.getStatus() != 200) {

            return new DDMSResult("400", "网络异常，请稍后重试");
        } else {
            JSONObject json = (JSONObject) JSONSerializer.toJSON(result);
            String status = json.getString("status");
            switch (status) {
                case "1":
                    return new DDMSResult("200", "登录成功");
                case "030":
                    return new DDMSResult("030", "用户名不能为空");
                case "031":
                    return new DDMSResult("031", "密码不能为空");
                case "032":
                    return new DDMSResult("032", "用户名或密码错误");
                case "033":
                    return new DDMSResult("033", "邮箱不能为空");
                case "034":
                    return new DDMSResult("034", "密码不匹配");
                case "035":
                    return new DDMSResult("035", "用户名已经存在");
                case "036":
                    return new DDMSResult("036", "邮箱已经被使用");
                case "999":
                    return new DDMSResult("999", "未知错误");
                default:
                    return new DDMSResult("999", "未知错误");
            }
        }
    }

}
